### test for 2 groups
### my custom program for testing 2 groups of data

# test of normal distribution (big P indicate normal distribution)
seiki1 = ks.test(sample_distribution, "pnorm", mean=mean(sample_distribution), sd=sd(sample_distribution))
seiki2 = ks.test(control_distribution, "pnorm", mean=mean(control_distribution), sd=sd(control_distribution))

seiki1$p.value
seiki2$p.value

# test of equal distribution (big P indicate variances are equal) 
bunsan = var.test(sample_distribution, control_distribution)
bunsan$p.value


if(seiki1$p.value > 0.05 && seiki2$p.value > 0.05 && bunsan$p.value > 0.05){
	# normal distribution & equal distribution = student t-test
	test = t.test(sample_distribution, control_distribution, alternative="greater", var.equal=T)
	test_name = "student t test"
}else if(seiki1$p.value > 0.05 && seiki2$p.value > 0.05 && bunsan$p.value < 0.05){
	# normal distribution & not equal distribution = welch t-test
	test = t.test(sample_distribution, control_distribution, alternative="greater", var.equal=F)
	test_name = "welch t test"
}else {
	# not normal distribution = wilcoxon ranking test
	test = wilcox.test(sample_distribution, control_distribution, alternative="greater")
	test_name = "wilcoxon test"
}

pvalue = test$p.value




### test for more than 2 groups (anova)

a1<-c(63,58,64,58,77,66,52,64,49,66)
a2<-c(64,64,68,61,56,71,64,65,85,75)
a3<-c(59,87,79,71,65,65,65,71,74,58)
a4<-c(83,79,65,67,80,72,80,75,72,84)
data <- data.frame(category=factor(c(rep("a1",10),rep("a2",10),rep("a3",10),rep("a4",10))),result=c(a1,a2,a3,a4))
boxplot(result ~ category, data=data, col="lightblue")
oneway.test(result ~ category, data=data, var=T)

# To find the which combination has difference.
TukeyHSD(aov(result ~ category, data=data))




### Pearson's chi-square test and Fisher's exact test
data <- matrix(c(14,8,4,17), ncol=2, byrow=T)
data
chisq.test(data)
fisher.test(data)





